<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\material\video;

use app\enum\AnalysisEnum;
use app\model\mongo\MongoMaterialVideoDayStatModel;
use app\service\analysis\AnalysisService;
use app\service\analysis\data\material\stat\DayStatTrait;
use app\service\analysis\material\VideoMaterialService;
use app\service\analysis\report\ReportService;
use app\service\BaseService;
use think\Model;

final class VideoDayStatService extends BaseService
{
    use DayStatTrait;

    public function __construct(int $mediaId = 0, string $signature = '', int $statDate = 0)
    {
        $this->mediaId = $mediaId;
        $this->signature = $signature;
        $this->statDate = $statDate;
    }

    /**
     * 设置数据服务
     */
    protected function setReport(): bool
    {
        $this->reportService = AnalysisService::getService($this->mediaId, AnalysisEnum::DATA_REPORT, AnalysisEnum::TYPE_VIDEO);
        if (! $this->reportService instanceof ReportService) {
            return self::setError('获取报表服务失败');
        }
        return true;
    }

    /**
     *  设置素材信息.
     */
    protected function setMaterial(): bool
    {
        $this->material = VideoMaterialService::getVideoBySignature($this->signature);
        if (! $this->material instanceof Model) {
            return self::setError('未知素材类型');
        }
        return true;
    }

    /**
     * 获取日汇总数据.
     * @return array|mixed|Model|MongoMaterialVideoDayStatModel
     */
    protected function getDayStatModel()
    {
        return (new MongoMaterialVideoDayStatModel())
            ->where([
                ['stat_date', '=', $this->statDate],
                ['signature', '=', $this->signature],
            ])
            ->findOrEmpty();
    }
}
